async function instantiate(module, imports = {}) {
    const adaptedImports = {
        env: Object.assign(Object.create(globalThis), imports.env || {}, {
            abort(message, fileName, lineNumber, columnNumber) {
                message = __liftString(message >>> 0);
                fileName = __liftString(fileName >>> 0);
                lineNumber = lineNumber >>> 0;
                columnNumber = columnNumber >>> 0;
                throw Error(`${message} in ${fileName}:${lineNumber}:${columnNumber}`);
            },
        }),
    };

    try {
        const { instance } = await WebAssembly.instantiate(module, adaptedImports);
        
        // 添加调试信息
        console.log('WebAssembly instance:', instance);
        console.log('WebAssembly instance exports:', instance.exports);

        const exports = instance.exports;

        // 检查导出的内存
        if (!exports.memory) {
            throw new Error('Memory not found in WebAssembly exports');
        }

        // 检查导出的函数
        if (typeof exports.calculateSinSumWithArray !== 'function') {
            throw new Error('Function calculateSinSumWithArray not found in WebAssembly exports');
        }

        const memory = exports.memory;
        const calculateSinSumWithArray = exports.calculateSinSumWithArray;

        // 创建一个简单的数组传递给 WebAssembly 函数
        const numbers = new Array(1000).fill(0).map((_, index) => index + 1);

        // 将 JavaScript 数组转换为 WebAssembly 内存中的数组
        const arrayBuffer = new Int32Array(memory.buffer, 0, numbers.length);
        for (let i = 0; i < numbers.length; i++) {
            arrayBuffer[i] = numbers[i];
        }

        // 调用 WebAssembly 函数并传递数组
        const result = calculateSinSumWithArray(arrayBuffer.byteOffset);

        // 打印结果和耗时
        console.log(`Result: ${result}`);
    } catch (error) {
        console.error('Error instantiating WebAssembly module:', error);
        throw error;
    }
}

(async function() {
    // 使用 d8 的 read 方法读取文件内容
    const buffer = read('./sin_sum2.wasm', 'binary');
    const module = new Uint8Array(buffer);

    try {
        await instantiate(module);
    } catch (error) {
        console.error('Error running WebAssembly function:', error);
    }
})();